package com.example.loginlib

import android.app.Application
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.baselib.BaseViewModel
import com.example.baselib.activity.MyDao
import com.example.baselib.databean.Unitinfo
import com.example.baselib.databean.UserData
import com.example.baselib.utils.UnitRoomDatabase


/**
 * Author: sxygsj
 * Date: 2020/4/14 14:39
 * Description: viewmodel
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class LoginViewModel(application: Application):BaseViewModel(application) {
    private val TAG = LoginViewModel::class.java.simpleName
    private val userdata: MutableLiveData<UserData> by lazy { MutableLiveData<UserData>()}
    private val repository=LoginRepository()
    val mDao: MyDao by lazy {  UnitRoomDatabase.getDatabase(application).myDao()}
    fun getLoginDatas(): LiveData<UserData> {
        return userdata

    }
    fun loginByPwd(uniqueid:String, comename:String, password:String)=launchUI {
        val result =repository.loginByPwd(uniqueid,comename,password)
//        val insertAll= mDao.insertAll(result.body()?.unitinfo as MutableList<Unitinfo>)

//        Log.i("database+++++++",insertAll?.size.toString())
        userdata.value=result.body()
    }

    fun getVerificationCode(tel: String) =launchUI{
        val result=repository.getLoginVerCode(tel)
        Toast.makeText(getApplication<Application>(),result.body()?.msg,Toast.LENGTH_SHORT).show()
    }
    fun loginByTel(tel: String,verCode:String)=launchUI{
        val result= repository.loginByTel(tel,verCode)
        userdata.value=result.body()
    }
}